package _220323;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author ShadowLim
 * @create 2022-03-23-11:29
 */

/**
 * next()方法一定要读取到有效字符之后才可以结束输入 对于有效字符之前的空格/Tab/Enter键自动将其去掉
 * 读取到有效字符之后才将空格/Tab/Enter键作为分隔符或结束符
 */
public class 穿越雷区 {
    static int n;
    static int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    static char[][] map = new char[101][101];
    static int stX, stY;
    static boolean[][] vis = new boolean[101][101];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            String str = scanner.next();
            for (int j = 0; j < n; j ++) {
                map[i][j] = str.charAt(0);
                if (map[i][j] == 'A') {
                    stX = i;
                    stY = j;
                }
            }
        }
        dfs(stX, stY);
    }

    private static void dfs(int x, int y) {
        Queue<Node> q = new LinkedList<>();
        int r, c, step;
        Node root = new Node(x, y, 0);
        vis[x][y] = true;
        q.offer(root);
        while (!q.isEmpty()) {
            Node node = q.poll();
            r = node.x;
            c = node.y;
            step = node.step;
            for (int i = 0; i < 4; i++) {
                int nx = r + dirs[i][0];
                int ny = c + dirs[i][1];
                if (nx >= n || nx < 0 || ny >= n || ny < 0) {
                    continue;
                }
                if (!vis[nx][ny]) {
                    if (map[r][c] != map[nx][ny]) {
                        Node nnode = new Node(nx, ny, step + 1);
                        q.offer(nnode);
                        vis[nx][ny] = true;
                        if (map[nx][ny] == 'B') {
                            System.out.println(nnode.step);
                            return;
                        }
                    }
                }
            }
        }
        System.out.println(-1);
        return;
     }
}

class Node {
    int x, y, step;

    public Node(int x, int y, int step) {
        this.x = x;
        this.y = y;
        this.step = step;
    }
}
